import { api } from '@/servers/api'
import { getData, postData } from '@/servers/http'
import { getLocalStorage } from '@/storage'
import { makeAutoObservable, runInAction } from 'mobx'
import wx from 'weixin-webview-jssdk'

class Store {
    /** 是否已完成档案 */
    public isFinished = false
    /** 请求接口获取到的五指图数据 */
    public chartList = []
    /** 填写的五指图数据 */
    public fillInList: { name: string; feature: string }[] = []

    constructor() {
        makeAutoObservable(this)
    }
    /** 设置填写数据 */
    setFillInList = (data: never[]) => {
        this.fillInList = data || []
    }
    /** 获取五指图详情 */
    getFiveFingerCharts = () => {
        getData(api.fiveFingerCharts).then(({ status, data }) => {
            if (status === 200 && data) {
                runInAction(() => {
                    this.chartList = data || []
                })
            }
        })
    }
    /** 保存五指图 */
    submitFiveFingerCharts = () => {
        const identityType = getLocalStorage('IDENTITY_TYPE')
        postData(api.fiveFingerCharts, { data: this.fillInList }).then(({ status, data }) => {
            if (status === 200) {
                runInAction(() => {
                    this.isFinished = data?.is_first_view === '1' ? false : true
                })
                this.getFiveFingerCharts()

                if (Number(identityType) !== 2 && data?.gain_medal) {
                    wx.miniProgram.navigateTo({
                        url: `/pageStudent/pages/gotMedal/index?medalData=${encodeURIComponent(JSON.stringify(data.gain_medal))}`,
                    })
                }
            }
        })
    }
    /** 回到第一步 */
    backToFirst = () => {
        this.isFinished = false
        this.fillInList = this.chartList.map((i: any) => ({ name: i.name, feature: i.feature }))
        this.chartList = []
    }
    /** 清除数据 */
    clearData = () => {
        this.isFinished = false
        this.chartList = []
        this.fillInList = []
    }
}

export default new Store()
